package com.ljb.android.component.project

import android.content.Context
import com.blankj.utilcode.util.SPStaticUtils
import com.ljb.android.comm.BuildConfig
import com.ljb.android.comm.NetworkLoggingInterceptor
import com.ljb.android.comm.weiget.page.PageStateLayout
import com.ljb.android.component.project.common.ProjectConstant
import net.ljb.kt.HttpConfig
import net.ljb.kt.utils.NetLog

object ProjectInit {

    fun init(context: Context) {
        initPage()
        initNet()
    }

    private fun initNet() {
        HttpConfig.Builder(BuildConfig.HTTP_HOST)
            .addInterceptor(NetworkLoggingInterceptor(object : NetworkLoggingInterceptor.Logger {
                override fun log(message: String?) {
                    NetLog.e(message)
                }
            }).setLevel(NetworkLoggingInterceptor.Level.BODY))
            .addCommCookie(object : HttpConfig.ICommCookie {
                //Cookie 持久化
                override fun saveCookie(host: String, cookie: String) {
                    SPStaticUtils.put(host, cookie)
                }

                override fun loadCookie(host: String): String? {
                    return SPStaticUtils.getString(host)
                }
            }).addCommHeader { headers ->
                headers["from"] = "projectHeader"
            }.addCommParam { params ->
                params["from"] = "projectParam"
            }.openLog(BuildConfig.DEBUG)
            .newClientByTag(ProjectConstant.MODULE_NAME)
            .build()
    }

    private fun initPage() {
        PageStateLayout.apply {
            setDefaultEmptyLayout(R.layout.project_layout_empty, ProjectConstant.MODULE_NAME)
            setDefaultErrorLayout(R.layout.project_layout_error, ProjectConstant.MODULE_NAME)
            setDefaultLoadLayout(R.layout.project_layout_loading, ProjectConstant.MODULE_NAME)
        }
    }
}